﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameBase
{
    public class NotificationCenter
    {
        public static List<ITopic> _topics = new List<ITopic>();

        public static void addNewTopic(ITopic topic)
        {
            _topics.Add(topic);
        }

        private static int findTopicWithName(string topicName)
        {
            for (int i = 0; i < _topics.Count; i++)
            {
                if (_topics[i].TopicName == topicName)
                {
                    return i;
                }
            }
            return -1;
        }

        public static void subscribe(string topicName, ISubscriber subscriber)
        {
            int topicIdx = findTopicWithName(topicName);
            if (topicIdx == -1)
                return;
            _topics[topicIdx].addSubscriber(subscriber);                
        }

        public static void unSubscribe(string topicName, ISubscriber subscriber)
        {
            int topicIdx = findTopicWithName(topicName);
            if (topicIdx == -1)
                return;
            _topics.RemoveAt(topicIdx);
        }

        public static void postNotification(string topicName, string message)
        {
            int topicIdx = findTopicWithName(topicName);
            if (topicIdx == -1)
                return;
            _topics[topicIdx].postNotification(message);
        }
    }
}
